<?php
/**
* author: bqs
* desc: 请求modbus地址
* 公众号： ZERO开发
*/

require_once 'vendor/adduc/phpmodbus/Phpmodbus/ModbusMaster.php';

	// Modbus master UDP
	$modbus = new ModbusMaster("127.0.0.1", "TCP");
	
	// Read multiple registers
	try {
		$recData = $modbus->readMultipleRegisters(1, 0, 5);
	}
	catch (Exception $e) {
		// Print error information if any
		echo $modbus;
		echo $e;
		exit;
	}
	
	// 数据库设备的数据描述
	$devicesDataBlock = [
		"0" => "weather",
		"1" => "water",
		"2" => "voice",
		"3" => "electric",
		"4" => "air"
	];
	
	$realData = [];
	
	foreach($recData as $key => $value) {
		$indexs = ($key-1)/2;
		if (($key-1)%2 == 0) {
			$realData[$devicesDataBlock[$indexs]] = $value;
		}
	}
	
	var_dump($realData);die;
	
	// Print data in string format
	echo PhpType::bytes2string($recData);
	
	// 00050000000601030000000A


?>